import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class CarNumberPageNew extends StatefulWidget {
  bool show;
  TextEditingController controller;
  String pro;
  String num = "";
  final changeText;
  Border border;
  Function onChange;

  @override
  _CarNumberPageState createState() => _CarNumberPageState();

  CarNumberPageNew(
      this.show, this.controller, this.pro, this.changeText, this.onChange,
      {this.border}) {
    if (controller != null) {
      num = controller.text;
    }
    if (border == null) {
      border = Border(bottom: BorderSide(width: 1, color: Color(0xffefefef)));
    }
  }
}

class _CarNumberPageState extends State<CarNumberPageNew> {
  List<String> datas = [
    '京',
    '津',
    '沪',
    '渝',
    '鲁',
    '冀',
    '晋',
    '蒙',
    '辽',
    '吉',
    '黑',
    '苏',
    '浙',
    '皖',
    '闽',
    '赣',
    '豫',
    '湘',
    '鄂',
    '粤',
    '桂',
    '琼',
    '川',
    '贵',
    '云',
    '藏',
    '陕',
    '甘',
    '青',
    '宁',
    '新',
    '港',
    '澳',
    '台',
  ];

  List<DropdownMenuItem> widgets = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    for (int i = 0; i < datas.length; i++) {
      widgets.add(DropdownMenuItem(
        child: Container(child: Text(datas[i])),
        value: datas[i],
      ));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.only(left: 16, right: 16, top: 15, bottom: 15),
      decoration: BoxDecoration(color: Colors.white, border: widget.border),
      child: Row(
        children: <Widget>[
          Text(
            "车牌号:",
            style: TextStyle(color: Color(0xff323232), fontSize: 16),
          ),
          Visibility(
            child: Container(
              height: 20,
              width: 48,
              margin: EdgeInsets.only(left: 10),
              padding: EdgeInsets.only(left: 10),
              color: Colors.blue,
              child: DropdownButtonHideUnderline(
                  child: DropdownButton(
                      items: widgets,
                      value: widget.pro,
                      elevation: 24,
                      style: TextStyle(
                        //设置文本框里面文字的样式
                        color: Color(0xff646464),
                        fontSize: 14,
                      ),
                      onChanged: (value) {
                        setState(() {
                          widget.pro = value;
                          widget.changeText(value);
                        });
                      })),
            ),
            visible: widget.show,
          ),
          Expanded(
            child: TextField(
                maxLines: 1,
                autocorrect: true,
                keyboardType: TextInputType.emailAddress,
                controller: TextEditingController.fromValue(TextEditingValue(
                    // 设置内容
                    text: widget.num,
                    // 保持光标在最后
                    selection: TextSelection.fromPosition(TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: widget.num.length)))),
                onChanged: (value) {
                  widget.onChange(value);
                  setState(() {
                    widget.num = value.toUpperCase();
                    widget.controller.text = value.toUpperCase();
                  });
                },
                inputFormatters: <TextInputFormatter>[
                  WhitelistingTextInputFormatter(RegExp(r'[a-zA-Z0-9]')),
                  //只输入数字
                  LengthLimitingTextInputFormatter(7)
                  //限制长度
                ],
                decoration: InputDecoration(
                    hintText: "请输入车辆车牌号",
                    hintStyle: TextStyle(fontSize: 16),
                    icon: Text(
                      "",
                      style: TextStyle(fontSize: 16),
                    ),
                    border: InputBorder.none,
                    contentPadding: EdgeInsets.only(right: 12, bottom: 4))),
          )
        ],
      ),
    );
  }
}
